********************************************************************
*                SUMMARY STATS ON SID/SEDD DATASET                 *
********************************************************************

log using "${SIDCodePath}/sidsedd_summarystats.log", replace
*--------------------------------------------------
* Summarize 2013Q2 variables
*--------------------------------------------------
	use edhour_2 dqtr year hcup_os age medicare trad_medicare dshospid ///
	 inpatient los totchg ndx died npr orproc tchg_lsvi age female sincevi_30 sincevi_60 sincevi_90 sinceIP_30 sinceIP_60 sinceIP_90 nchronic medincstq ///
	 pointoforigin_x race_x hispanic_x nxtvi_30d nxtvi_60d nxtvi_90d aweekend dqtr_2 year_dqtr after_MN obs out_notobs white hispanic using "${SIDDataPath}/sidsedd_merged_20102016.dta", clear

	
	// Summary stats of ED and inpatient patients who come in within 3 hours of midnight
	preserve
		keep if year_dqtr == 2013.5 & edhour_2 >= -300 & edhour_2 <= 200 & trad_medicare == 1

		
		label var inpatient "share inpatient"
		label var obs 	    "share observation"
		label var totchg    "average charges"
		label var age 	    "average age"
		label var female    "share female"
		label var sinceIP_30 "share inpatient in last 30 days"
		label var sincevi_30 "share hospital visit in last 30 days"
		label var nchronic  "average n of chronic conditions"
		label var white     "share white"
		label var hispanic  "share hispanic"
		label var aweekend  "share weekend"
		label var nxtvi_30d "share hospital visit in next 30 days"
		label var nxtvi_60d "share hospital visit in next 60 days"
		label var nxtvi_90d "share hospital visit in next 90 days"


		eststo clear
		/* estpost summarize inpatient obs totchg age  white hispanic female nchronic sinceIP_30 sincevi_30 nxtvi_30d nxtvi_60d nxtvi_90d, listwise
		eststo overall */
		estpost summarize inpatient obs totchg age  white hispanic female nchronic sinceIP_30 sincevi_30 nxtvi_30d nxtvi_60d nxtvi_90d if after_MN == 0 & edhour_2 >= -300 & edhour_2 <= 200, listwise
		eststo beforemn
		estpost summarize inpatient obs totchg age  white hispanic female nchronic sinceIP_30 sincevi_30 nxtvi_30d nxtvi_60d nxtvi_90d if after_MN == 1 & edhour_2 >= -300 & edhour_2 <= 200, listwise
		eststo aftermn

		esttab using "${SIDOutputPath}/SummaryStats/fl_sidsedd_trad_3h.tex", replace ///
			cells("mean(fmt(2))" "sd(fmt(2))")  mtitles("Before MN" "After MN" ) unstack sfmt(%9.1f) label
	restore 

	gen within3hours_before= edhour_2 >= -300 & edhour_2 <0
	gen within3hours_after= edhour_2 >0 & edhour_2 <= 200

	sum within3hours_before if year_dqtr == 2013.5 & trad_medicare == 1
	* 0.097
	
	sum within3hours_after if year_dqtr == 2013.5 & trad_medicare == 1
	* 0.03 */



*--------------------------------------------------
* Summarize 2010Q1 variables in order to compare to MEDPAR
*--------------------------------------------------
	use key edhour_2 dqtr year hcup_os age medicare trad_medicare dshospid ///
	 inpatient los totchg ndx died npr orproc tchg_lsvi age female sincevi_30 sincevi_60 sincevi_90 sinceIP_30 sinceIP_60 sinceIP_90 nchronic medincstq ///
	 pointoforigin_x race_x hispanic_x nxtvi_30d nxtvi_60d nxtvi_90d aweekend using "${SIDDataPath}/sidsedd_merged_20102016.dta", clear
	 
	// Generate post-policy dummy
	gen 	dqtr_2 						= (dqtr - 1)/4
	gen 	year_dqtr 					= year + dqtr_2 /* This value is defined s.t. 2012.0 = first quarter of 2012, 2012.25 = second quarter of 2012, etc... */
	// After-midnight dummy
	gen 	after_MN 					= 0
	replace after_MN 					= 1 ///
										if edhour_2 >= 0
	// Observation dummy (since hcup_os == 3 when observation status)
	gen 	obs 						= cond(hcup_os > 0, 1, 0)
	gen     out_notobs                  = cond(hcup_os == 0 & inpatient == 0, 1, 0)
	
	gen 	white = race == "5"
	gen  	hispanic = hispanic_x == "E1"

	//restrict to 2010 and traditional medicare only
	keep if year == 2010 & trad_medicare == 1

 	// merge in chronic conditions from severity file for inpatients
 	merge 1:1 key using "${SIDPath}/2010/fl_sid_2010_severity.dta", keep(1 3)
 	tab _merge

 	gen cm_diab = cm_dm == 1 | cm_dmcx == 1

 	label var age 	      "average age"
	label var female      "share female"
	label var sinceIP_30  "share inpatient in last 30 days"
	label var sinceIP_60  "share inpatient in last 60 days"
	label var sinceIP_90  "share inpatient in last 90 days"
	label var white       "share white"
	label var hispanic    "share hispanic"
	label var cm_aids     "share with cc: aids"
	label var cm_alcohol  "share with cc: alcohol"
	label var cm_arth     "share with cc: arthritis" 				 // need to check if this matches up with cms
	label var cm_chf      "share with cc: congestive heart failure"  // need to check
	label var cm_chrnlung "share with cc: chronic pulmonary disease" // need to check
	label var cm_diab     "share with cc: diabetes"
	label var cm_drug     "share with cc: drug abuse"
	label var cm_htn_c    "share with cc: hypertension"
	label var cm_renlfail "share with cc: renal failure"            // need to check
	label var cm_depress  "share with cc: depression"
	label var cm_psych    "share with cc: psychoses"                 // need to check

	eststo claer
 	// Summary stats of inpatient patients who come through the ED
 	estpost summarize age female sinceIP_30 sinceIP_60 sinceIP_90 white hispanic cm_aids cm_alcohol cm_arth cm_chf cm_chrnlung cm_diab cm_drug cm_htn_c cm_renlfail cm_depress cm_psych if inpatient == 1 , listwise
	eststo inpatient_2010

 	// Summary stats of inpatient patients who come through the ED within 3 hours of midnight
 	estpost summarize age female sinceIP_30 sinceIP_60 sinceIP_90 white hispanic cm_aids cm_alcohol cm_arth cm_chf cm_chrnlung cm_diab cm_drug cm_htn_c cm_renlfail cm_depress cm_psych if inpatient == 1 & edhour_2 >= -300 & edhour_2 <= 200, listwise
 	eststo inpatient_2010_3hrs


 	esttab using "${SIDOutputPath}/SummaryStats/fl_sidsedd_trad_IP_2010.tex", replace ///
		cells("mean(fmt(2))" "sd(par fmt(2))")  mtitles("All" "Within 3 Hours" ) unstack sfmt(%9.1f) label

	tab inpatient
	* 44% inpatient

	tab inpatient if edhour_2 >= -300 & edhour_2 <= 200
	* 42.5% inpatient


*--------------------------------------------------
* Summarize variables by quarter
*--------------------------------------------------
	use edhour_2 dqtr year hcup_os age medicare trad_medicare dshospid ///
	 inpatient los totchg ndx died npr orproc tchg_lsvi age female sincevi_30 sincevi_60 sincevi_90 sinceIP_30 sinceIP_60 sinceIP_90 nchronic medincstq ///
	 pointoforigin_x race_x hispanic_x nxtvi_30d nxtvi_60d nxtvi_90d aweekend using "${SIDDataPath}/sidsedd_merged_20102016.dta", clear

	// Generate post-policy dummy
	gen 	dqtr_2 						= (dqtr - 1)/4
	gen 	year_dqtr 					= year + dqtr_2 /* This value is defined s.t. 2012.0 = first quarter of 2012, 2012.25 = second quarter of 2012, etc... */
	// After-midnight dummy
	gen 	after_MN 					= 0
	replace after_MN 					= 1 ///
										if edhour_2 >= 0
	// Observation dummy (since hcup_os == 3 when observation status)
	gen 	obs 						= cond(hcup_os > 0, 1, 0)
	gen     out_notobs                  = cond(hcup_os == 0 & inpatient == 0, 1, 0)
	
	gen 	white = race == "5"
	gen  	hispanic = hispanic_x == "E1"

	// 3 hours around midnight, traditional medicare
		preserve
			keep if edhour_2 >= -300 & edhour_2 <= 200 & trad_medicare == 1
			gcollapse (mean) after_MN, by(year_dqtr)

			twoway scatter after_MN year_dqtr, ///
			xline(2013.75, lcolor(black) lpattern(dash)) yline(0) graphregion(color(white)) ///
				xtitle(Quarter) ytitle("Share ED visits after midnight") xlabel(2010(1)2016)

			graph export "${SIDOutputPath}/SummaryStats/q_med_3h_share_after_MN.eps", replace  
		restore 
		
		
		preserve
			keep if edhour_2 >= -300 & edhour_2 <= 200 & trad_medicare == 1
			gcollapse (mean) inpatient obs totchg age  white hispanic female nchronic sinceIP_30 sincevi_30 nxtvi_30d nxtvi_60d nxtvi_90d, by(year_dqtr after_MN)
			label var inpatient "share inpatient"
			label var obs 	    "share observation"
			label var totchg    "average charges"
			label var age 	    "average age"
			label var female    "share female"
			label var sinceIP_30 "share inpatient in last 30 days"
			label var sincevi_30 "share hospital visit in last 30 days"
			label var nchronic  "average n of chronic conditions"
			label var white     "share white"
			label var hispanic  "share hispanic"
			*label var aweekend  "share weekend"
			label var nxtvi_30d "share hospital visit in next 30 days"
			label var nxtvi_60d "share hospital visit in next 60 days"
			label var nxtvi_90d "share hospital visit in next 90 days"

			foreach var of varlist inpatient obs totchg age  white hispanic female nchronic sinceIP_30 sincevi_30 nxtvi_30d nxtvi_60d nxtvi_90d{
				local varlabel : variable label `var'
				if strpos("`var'", "nxtvi_") == 0 & strpos("`var'", "since") == 0 {
					twoway (connected `var' year_dqtr if after_MN == 0, mcolor(dknavy) lcolor(dknavy) lpattern(solid)) ///
					   (connected `var' year_dqtr if after_MN == 1, mcolor(maroon) lcolor(maroon) lpattern(dash)) , ///
					   legend(order(1 "Before-Midnight Arrivals" 2 "After-Midnight Arrivals")) ///
					  xline(2013.75, lcolor(black) lpattern(dash)) graphregion(color(white)) ///
					  xtitle(Quarter) ytitle("") xlabel(2010(1)2016) ///
			   	    subtitle("`varlabel'", position(11) justification(left)) ylabel(,angle(horizontal)) 
				}

				if strpos("`var'", "since") > 0{

					twoway (connected `var' year_dqtr if after_MN == 0  & year_dqtr >= 2010.25, mcolor(dknavy) lcolor(dknavy) lpattern(solid)) ///
					   (connected `var' year_dqtr if after_MN == 1 &year_dqtr >= 2010.25, mcolor(maroon) lcolor(maroon) lpattern(dash)) , ///
					   legend(order(1 "Before-Midnight Arrivals" 2 "After-Midnight Arrivals")) ///
					  xline(2013.75, lcolor(black) lpattern(dash)) graphregion(color(white)) ///
					xtitle(Quarter) ytitle("") xlabel(2010(1)2016) ///
			   	    subtitle("`varlabel'", position(11) justification(left)) ylabel(,angle(horizontal)) 
				}

				if strpos("`var'", "nxtvi_") > 0{
					twoway (connected `var' year_dqtr if after_MN == 0 & year_dqtr <= 2016.5, mcolor(dknavy) lcolor(dknavy) lpattern(solid)) ///
					   (connected `var' year_dqtr if after_MN == 1 & year_dqtr <= 2016.5, mcolor(maroon) lcolor(maroon) lpattern(dash)) , ///
					   legend(order(1 "Before-Midnight Arrivals" 2 "After-Midnight Arrivals")) ///
					  xline(2013.75, lcolor(black) lpattern(dash))  graphregion(color(white)) ///
					xtitle(Quarter) ytitle("") xlabel(2010(1)2016) ///
			   	    subtitle("`varlabel'", position(11) justification(left)) ylabel(,angle(horizontal)) 
				}
				

				graph export "${SIDOutputPath}/SummaryStats/q_med_3h_`var'.eps", replace  
			}
			
		restore 

	
*--------------------------------------------------
* Summarize pre- and post- variables
*--------------------------------------------------

	use key edhour_2 dqtr year hcup_os age medicare trad_medicare dshospid ///
	 inpatient los totchg ndx died npr orproc tchg_lsvi age female sincevi_30 sincevi_60 sincevi_90 sinceIP_30 sinceIP_60 sinceIP_90 nchronic medincstq ///
	 pointoforigin_x race_x hispanic_x nxtvi_30d nxtvi_60d nxtvi_90d aweekend using "${SIDDataPath}/sidsedd_merged_20102016.dta", clear

	// Merge in predicted risk
	merge 1:1 key using "${SIDDataPath}/riskpredict/fl_med_1016_all_pred.dta", keepusing(pred_B4) nogen

	// Generate post-policy dummy
	gen 	dqtr_2 						= (dqtr - 1)/4
	gen 	year_dqtr 					= year + dqtr_2 /* This value is defined s.t. 2012.0 = first quarter of 2012, 2012.25 = second quarter of 2012, etc... */
	// After-midnight dummy
	gen 	after_MN 					= 0
	replace after_MN 					= 1 ///
										if edhour_2 >= 0
	// Observation dummy (since hcup_os == 3 when observation status)
	gen 	obs 						= cond(hcup_os > 0, 1, 0)
	gen     out_notobs                  = cond(hcup_os == 0 & inpatient == 0, 1, 0)
	
	gen 	white = race == "5"
	gen  	hispanic = hispanic_x == "E1"

	
	gen 	pred_severity_B4 = exp(pred_B4)/(1+exp(pred_B4))

	sum pred_severity_B4
	assert pred_severity_B4 >= 0 & pred_severity_B4 <= 1 if trad_medicare == 1 & !missing(pred_severity_B4)
	

	label var inpatient "share inpatient"
	label var obs 	    "share observation"
	label var totchg    "average charges"
	label var age 	    "average age"
	label var female    "share female"
	label var sinceIP_30 "share inpatient in last 30 days"
	label var sincevi_30 "share hospital visit in last 30 days"
	label var nchronic  "average n of chronic conditions"
	label var white     "share white"
	label var hispanic  "share hispanic"
	label var aweekend  "share weekend"
	label var nxtvi_30d "share hospital visit in next 30 days"
	label var nxtvi_60d "share hospital visit in next 60 days"
	label var nxtvi_90d "share hospital visit in next 90 days"
	label var pred_severity_B4  "average predicted admission likelihood (demo + current dx)"
	
	
	// Summary stats of ED and inpatient patients who come in within 3 hours of midnight
	eststo clear
	preserve
		keep if year_dqtr == 2013.5 & edhour_2 >= -300 & edhour_2 <= 200 & trad_medicare == 1

		
		estpost summarize inpatient obs totchg age  white hispanic female nchronic sinceIP_30 sincevi_30 pred_severity_B4 nxtvi_30d nxtvi_60d nxtvi_90d if after_MN == 0 & edhour_2 >= -300 & edhour_2 <= 200, listwise
		eststo beforemn_pre
		estpost summarize inpatient obs totchg age  white hispanic female nchronic sinceIP_30 sincevi_30 pred_severity_B4 nxtvi_30d nxtvi_60d nxtvi_90d if after_MN == 1 & edhour_2 >= -300 & edhour_2 <= 200, listwise
		eststo aftermn_pre

	restore 

	preserve
		keep if year_dqtr == 2014.5 & edhour_2 >= -300 & edhour_2 <= 200 & trad_medicare == 1

		estpost summarize inpatient obs totchg age  white hispanic female nchronic sinceIP_30 sincevi_30 pred_severity_B4 nxtvi_30d nxtvi_60d nxtvi_90d if after_MN == 0 & edhour_2 >= -300 & edhour_2 <= 200, listwise
		eststo beforemn_post
		estpost summarize inpatient obs totchg age  white hispanic female nchronic sinceIP_30 sincevi_30 pred_severity_B4 nxtvi_30d nxtvi_60d nxtvi_90d if after_MN == 1 & edhour_2 >= -300 & edhour_2 <= 200, listwise
		eststo aftermn_post

	restore

	esttab using "${SIDOutputPath}/SummaryStats/fl_sidsedd_trad_pre_post_3h.tex", replace ///
			cells("mean(fmt(2))" "sd(fmt(2) par)")  mtitles("Before MN (2013.5)" "After MN (2013.5)" "Before MN (2014.5)" "After MN (2014.5)" ) unstack sfmt(%9.1f) label
	

log close

